package db;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Provides a connection for the database and handles executing queries
 * @author Alex
 */
public class DatabaseConnector  {
	
    public static Connection con = null;
    public static boolean debug = false;
    
    // default values
    public String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    public String username = "root";
    public String password = "";
    public String dbname = "jdbc:mysql://localhost/snort_teacher";
    public String driver = "com.mysql.jdbc.Driver";
    
    public DatabaseConnector(String userd , String passd, String dbname){
        this.password = passd;
        this.username= userd;
        this.dbname = dbname;
    }
    
    public DatabaseConnector(){
    	loadProperties();
    }

    public Connection getConnection(){    	
    	if(con == null){
    		con = connect();
    	}
    	return con;
    }
    
    public boolean closeConnection(){
    	try{
    		getConnection().close();
    	}
    	catch(SQLException ex){
    		ex.printStackTrace();
    		print("Could not close SQL Connection. Cause: " + ex.getMessage());
    		return false;
    	}
    	con = null;
    	return true;
    }
    
    public static boolean commit(){
    	try{
    		con.commit();
    	}
    	catch(SQLException ex){
    		ex.printStackTrace();
    		return false;
    	}
    	return true;
    }
    
    public ResultSet executeQuery(String query){
    	ResultSet rs = null;
    	print("executing query '"+query+"'");
    	try{
    		rs = getConnection().createStatement().executeQuery(query);
    		print("database connection closed");
    	}
    	catch(SQLException ex){
    		ex.printStackTrace();
    	}
    	
    	return rs;
    }
    public int executeUpdate(String query){
    	int rowsAffected = -1;
    	print("executing query '"+query+"'");
    	try{
    		rowsAffected = getConnection().createStatement().executeUpdate(query);
    		print("database connection closed");
    	}
    	catch(SQLException ex){
    		ex.printStackTrace();
    	}
    	

    	return rowsAffected;
    }
    
    public Connection connect() {
    	try {
    		print("entered connect() in DatabaseConnetor");
            Class.forName(JDBC_DRIVER);
            con = DriverManager.getConnection(dbname,username,password);
            print ("database connection established");
        }
        catch (Exception e) {
            e.printStackTrace();
            print("could not connect to the database '"+dbname+"'. Cause: "+ e.getMessage());
        }
    	return con;
    }
    
    public void loadProperties (){
	    Properties properties = new Properties();
	
	    try {
	        properties.load(new FileInputStream("src/db.properties"));

	        driver = properties.getProperty("driver");
	        username = properties.getProperty("username");
	        password = properties.getProperty("password");
	        dbname = properties.getProperty("dbname");
	    } catch (IOException e) {
	    	System.out.println("Exception occurred" + e.getMessage());
	    	System.out.println("Using default values instead");
	    	return;
	    }
	        print("===================================");
	        // Display all the values in the form of key value
	        for (String key : properties.stringPropertyNames()) {
	
	        	String value;
	        	if(key.equals("password")){
	        		value = "********";
	        	}else{
	        		value = properties.getProperty(key);
	        	}
	            
	            print(key + "=" + value);
	        }
	        print("===================================");
    }
    
    private static void print(String value){
    	if(debug){
    		System.out.println(value);
    	}
    }
}